from django.db import models

# Create your models here.
class Swiped(models.Model):
    uid = models.IntegerField(verbose_name="用户自身id")
    sid = models.IntegerField(verbose_name="被滑的陌生人id")
    mark = models.CharField(max_length=30, verbose_name="滑动类型")
    time = models.DateTimeField(verbose_name="滑动的时间", auto_now_add=True)

    class Meta:
        db_table = 'sp_swiped'

    @classmethod
    def add_swiped(cls, uid, sid, mark):
        cls.objects.create(
            uid=uid,
            sid=sid,
            mark=mark
        )

        # uid=6, sid=4
        if mark in ['like', 'superlike']:
            if cls.objects.filter(uid=sid, sid=uid).exists():
                uid1 = min([uid, sid])
                uid2 = max([uid, sid])
                Friend.objects.create(
                    uid1=uid1,
                    uid2=uid2
                )

    def __str__(self):
        return str(self.id)



class Friend(models.Model):
    uid1 = models.IntegerField()
    uid2 = models.IntegerField()

    class Meta:
        db_table = 'sp_friend'





class Vip(models.Model):
    name = models.CharField(max_length=50)
    level = models.IntegerField()
    # max_digits 整数位+小数位的长度, decimal_places 小数位的长度
    price = models.DecimalField(max_digits=7, decimal_places=2)

    class Meta:
        db_table = 'sp_vip'


class Permission(models.Model):
    name = models.CharField(max_length=30)
    description = models.CharField(max_length=100)

    class Meta:
        db_table = 'sp_permission'


class VipPermRelation(models.Model):
    vip_id = models.IntegerField()
    perm_id = models.IntegerField()

    class Meta:
        db_table = 'sp_vippermrelation'